#include "ServerCal.hpp"
#include "TcpServer.hpp"

void Usage(const std::string &proc)
{
    std::cout << "\r\n Usage: " << proc << " Serverport Serverip" << std::endl;
}

//测试 Request 报文格式是否正确
void TestRequest()
{
    //序列化 + 添加报头
    Request req(1000, 0, '+');
    std::string pakeage;
    req.serialize(&pakeage);
    std::cout << pakeage << std::endl;
    pakeage = Encode(pakeage);
    std::cout << pakeage << std::endl;

    //去报头 + 反序列化
    std::string content;
    Decode(pakeage, &content);
    std::cout << content << std::endl;
    req.Deserialize(content);
}

//测试 Response 报文格式是否正确
void TestReponse()
{
    //序列化 + 添加报头
    Response rsp(666, 44);
    std::string pakeage;
    rsp.serialize(&pakeage);
    std::cout << pakeage << std::endl;
    pakeage = Encode(pakeage);
    std::cout << pakeage << std::endl;

    //去报头 + 反序列化
    std::string content;
    Decode(pakeage, &content);
    std::cout << content << std::endl;
    rsp.Deserialize(content);
}

//测试ServerCal.hpp功能
void TestCalculator()
{
    ServerCal SC;
    Request req(10, 20, '+');
    std::string pakeage;
    req.serialize(&pakeage);
    std::cout << pakeage << std::endl;

    std::string content;    
    content = Encode(pakeage);
    std::cout << content << std::endl;
    pakeage = "";
    pakeage = SC.Calculator(content);
    std::cout << pakeage << std::endl;

    //去报头 + 反序列化
    content = "";
    Decode(pakeage, &content);
    Response rsp;
    rsp.Deserialize(content);
    std::cout << content << std::endl;

    rsp.Printret();
}

int main(int argc, char *argv[])
{
    // ./ServerCal Serverport
    if(argc != 2)
    {
        Usage(argv[1]);
        exit(0);
    }

    uint16_t Serverport = stoi(argv[1]);

    // TestRequest();
    // TestReponse();
    // TestCalculator();

    TcpServer tser(Serverport, std::bind(&ServerCal::Calculator, ServerCal(), placeholders::_1));
    tser.Serverinit();
    tser.start();
}